import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  
  routes: [
    {
      path: '/',
      redirect: '/dashboard'
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component:() => import('@/views/Dashboard/index.vue'),
      children:[
        { 
          path: 'user',
          name: 'user',
          meta:{label:'用户管理'},
          
          component:() => import('@/views/Dashboard/user/User.vue'),
          children:[
            {
              path: 'index',
              name: 'index',
              meta:{label:'用户列表'},
              component:() => import('@/views/Dashboard/user/index.vue'),
            }
          ]
        }
      ]
    
    },
    {
      path: '/login',
      name: 'login',
      component:() => import('@/views/Login.vue')
    },
  ]
})


router.beforeEach((to, from) => {
  // ...
  const jwt = localStorage.getItem('jwt')
// console.log('userinfo',userinfo);
  if(to.path == '/login'){
    if(jwt) {
      // ElMessage({
      //   message: '已登陆',
      //   type: 'success',
      //   duartion:1000
      // })
      return { path: '/dashboard'}
    }
  }else {
    if(!jwt) {
      // ElMessage('请先登录')
      return { path: '/login' }
    }
  }
  // 返回 false 以取消导航
  // return false
})
export default router
